package com.example.demo.controller;

import com.example.demo.dao.CustomerRep;
import com.example.demo.model.Customer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customerController")
public class CustomerController {

    private CustomerRep customerRep;

    public CustomerController(CustomerRep customerRep) {
        this.customerRep = customerRep;
    }

    @GetMapping("/findById/{id}")
    public Customer findById(@PathVariable Long id){
        Customer customer = customerRep.findByCustomerId(id);
        return customer;
    }

    @GetMapping("/findAll")
    public List<Customer> findAll(){
        return customerRep.findAll();
    }

    @PostMapping("/save")
    public Customer saveCustomer(@RequestBody Customer customer){
        System.out.println(customer);
        return customerRep.save(customer);
    }

    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable Long id){
        if (!customerRep.existsById(id)){
            return "删除失败！没有该Id！";
        }
        customerRep.deleteById(id);
        return "删除成功!";
    }

    @PutMapping("/update/{id}")
    public Customer update(@PathVariable Long id, @RequestBody Customer customer){
        customer.setCustomerId(id);
        return customerRep.save(customer);
    }
}
